package com.shanghaionstar.user.service;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.onstar.cn.api.UsersService;
import com.onstar.cn.auth.userProfile.Vehicle;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by 7828wc on 16/8/30.
 */
public class ChangeVehicleService extends BaseService<List<Vehicle>> {
    private Context context;

    public ChangeVehicleService(Context context) {
        this.context = context;
    }

    @Override
    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public RequestEntity getRequestEntity() {
        UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);

        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);

        requestEntity = service.getVehicles(requestEntity, ApplicationUtil.getInstance().getSubscriberID());
        requestEntity = HttpInjectUtils.injectHttp(getClass(), "getRequestEntity", requestEntity);

        return requestEntity;
    }

    @Override
    public List<Vehicle> jsonToBean(String json) {
        return new Gson().fromJson(json,new TypeToken<ArrayList<Vehicle>>(){}.getType());
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public Observable<Vehicle> changeAccount(final String accountID,final String vin,final String body) {
        return Observable.defer(new Func0<Observable<Vehicle>>() {
            @Override
            public Observable<Vehicle> call() {
                try {
                    com.onstar.cn.api.v2.UsersService service = new com.onstar.cn.api.v2.UsersService(Constants.CURRENT_SERVER_URL);
                    RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
                    requestEntity.setBody(body);
                    requestEntity = service.swichVehicle_New(requestEntity, accountID);
                    requestEntity = HttpInjectUtils.injectHttp(ChangeVehicleService.this.getClass(), "changeAccount", requestEntity,String.class,String.class,String.class);
//                    com.onstar.cn.api.v2.UsersService service = new com.onstar.cn.api.v2.UsersService(Constants.CURRENT_SERVER_URL);
//                    RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);;
//                    LinkedHashMap<String, Object> coHashMap = new LinkedHashMap<String, Object>();
//                    coHashMap.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
//                    requestEntity.setCustomHeads(coHashMap);
//                    requestEntity = service.swichVehicle(requestEntity, ApplicationUtil.getInstance().getSubscriberID(), accountID, vin);
//                    requestEntity = HttpInjectUtils.injectHttp(ChangeVehicleService.this.getClass(), "changeAccount", requestEntity,String.class,String.class,String.class);

                    return Observable.just(GsonHelper.fromJson( BusinessUtil.getResponse(getDatas(requestEntity), context),Vehicle.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

}
